<?php

namespace App\Models\templates;;


use App\Models\Model;


/**
 * 配送模板区域及运费模型
 * Class DeliveryRule
 *
 * @package app\store\model
 * @property int $id 规则id
 * @property int $delivery_id 配送模板id
 * @property string $region 可配送区域(城市id集)
 * @property float $first 首件(个)/首重(Kg)
 * @property float $first_fee 运费(元)
 * @property float $additional 续件/续重
 * @property float $additional_fee 续费(元)
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read string $region_content
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule whereAdditional($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule whereAdditionalFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule whereDeliveryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule whereFirst($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule whereFirstFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule whereRegion($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\DeliveryRule whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read mixed $region_data
 */
class DeliveryRule extends Model
{

    protected $fillable = [
        'delivery_id',
        'region',
        'first',
        'first_fee',
        'additional',
        'additional_fee',
    ];


    protected $append = ['region_content'];

    static $regionAll;
    static $regionTree;

    /**
     * 可配送区域
     * @param $value
     * @param $data
     * @return string
     */
    public function getRegionContentAttribute($value)
    {

        // 当前区域记录转换为数组
        $regionIds = explode(',', $this->region);

        if (count($regionIds) === 373) return '全国';

        // 所有地区
        if (empty(self::$regionAll)) {
            self::$regionAll = Region::getCacheAll();
            self::$regionTree = Region::getCacheTree();
        }
        // 将当前可配送区域格式化为树状结构
        $alreadyTree = [];
        foreach ($regionIds as $regionId)
            $alreadyTree[self::$regionAll[$regionId]['pid']][] = $regionId;
        $str = '';
        foreach ($alreadyTree as $provinceId => $citys) {
            $str .= self::$regionTree[$provinceId]['name'];
            if (count($citys) !== count(self::$regionTree[$provinceId]['city'])) {
                $cityStr = '';
                foreach ($citys as $cityId)
                    $cityStr .= self::$regionTree[$provinceId]['city'][$cityId]['name'];
                $str .= ' (<span class="am-link-muted">' . mb_substr($cityStr, 0, -1, 'utf-8') . '</span>)';
            }
            $str .= '、';
        }
        return mb_substr($str, 0, -1, 'utf-8');
    }


    public function getRegionDataAttribute()
    {
        return explode(',', $this->region);
    }
}
